package menu;

import java.awt.Container;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;


/**
 * @author Anand 
 */
public class RightClickPopupDemo extends JFrame {

	JTextArea jtextarea;
	Container contentarea;
	JPopupMenu popup;
	Clipboard clipboard;
	
	
	public RightClickPopupDemo(){
		super("Right Click Popup Demo");
		
		clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		
		contentarea = getContentPane();
	
		jtextarea = new JTextArea(10,10);
		
		popup = new JPopupMenu();

		JMenuItem popupMenuItemCut = new JMenuItem("Cut");
		JMenuItem popupMenuItemCopy = new JMenuItem("Copy");
		JMenuItem popupMenuItemPaste = new JMenuItem("Paste");
		JMenuItem popupMenuItemExit = new JMenuItem("Exit");
		
		ActionListener actionListener = new MenuItemListener(jtextarea, clipboard);
		
		popupMenuItemCut.addActionListener(actionListener);
		popupMenuItemCopy.addActionListener(actionListener);
		popupMenuItemPaste.addActionListener(actionListener);
		popupMenuItemExit.addActionListener(actionListener);
		
		popup.add(popupMenuItemCut);
		popup.add(popupMenuItemCopy);
		popup.add(popupMenuItemPaste);
		popup.add(new JSeparator());
		popup.add(popupMenuItemExit);
		
		
		jtextarea.addMouseListener(new PopupListener(popup));
		
		int vsb = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
		int hsb = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
		
		contentarea.add(new JScrollPane(jtextarea, vsb,hsb));
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300,300);
	}
	
/*
	class PopupListener extends MouseAdapter {
	    public void mousePressed(MouseEvent e) {
	        showPopup(e);
	    }

	    public void mouseReleased(MouseEvent e) {
	        showPopup(e);
	    }

	    private void showPopup(MouseEvent e) {
	        if (e.isPopupTrigger()) {
	            popup.show(e.getComponent(),
	                       e.getX(), e.getY());
	        }
	    }
	}

	class MenuItemListener implements ActionListener{
		
		public void actionPerformed(ActionEvent actevent) {
			String cmd = actevent.getActionCommand();
			char choice = cmd.charAt(1);
			String text = "";
			switch(choice){
				case 'u':{
					text=jtextarea.getSelectedText();
					jtextarea.replaceRange("",jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());					
					StringSelection stringselected = new StringSelection(text);
					clipboard.setContents(stringselected, null);
					break;
				}
				case 'o':{
					text=jtextarea.getSelectedText();
					StringSelection stringselected = new StringSelection(text);
					clipboard.setContents(stringselected, null);
					break;
				}
				case 'a':{
					
					Transferable transdata = clipboard.getContents(null);
					try {
						String pasteString = (String)transdata.getTransferData(DataFlavor.stringFlavor);
						jtextarea.replaceRange(pasteString,jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());
					} catch (Exception e) {						
						System.out.println("Exception "+e);
					} 
					break;
				}
				case 'x':{					
					System.exit(0);
					break;
				}
			}
		}
		
	}
	*/
	
	public static void main(String[] args) {
		new RightClickPopupDemo().setVisible(true);
	}
}

